<template>
	<view class="content " :style="{paddingTop: yemiangao+'px'}">
		<view class="d-kuan-juzhong">
			<!--头部-->
			<view class="d-kuan pc-dingbu">
				<DToubu 
				ref="dToubu" 
				@login-success="handleLoginSuccess"
				@clear-audio="clearAllAudio" 
				@logout-success="handleLogout"
				:dengluxianshi="denglu_xianshi"
				@update-dengluxianshi="handleUpdateDengluxianshi"
				></DToubu>
			</view>

			<!--头部-->
			<!--下面内容-->
			<view class="d-kuan d-clear">
				<uv-row customStyle="margin-bottom: 10px" :gutter="10">
					<uv-col span="2" class="pc-yincang">
						<!--左边导航-->
						<view class="d-daohang d-you-10 pc-yincang d-shang-20" id="zuo-daohang" :style="{height: leftNavHeight}">
							<!-- 左边一级导航 -->
							<view class="d-daohang-5">
								<Ddaohang @denglu-fabu="denglu_fabu"></Ddaohang>
							</view>
						</view>
						<!--左边导航-->

					</uv-col>
					<uv-col  :span="kuan_shu" id="daohangkuan">
						<view class="pc-neikuang-20 ">
							<view class="daohang-guding" id="daohanggao" 
								:style="{
								    'position': 'fixed', 
								    'top': daohang_top+'px', 
								    'z-index': 99, 
								    'background': '#f3f3f3',
								    'padding': '10rpx',
								    'width': daohangkuanzhi   // 替换未定义的rightColWidth
								}">
								    <uv-tabs :list="yingyong_daohang_list" @click="diyiji" current="4" lineColor="#07bfa6" :activeStyle="{ color: '#0a9a87' }"></uv-tabs>
									<uv-tabs :list="shuxing_list" @click="fenlei_ck" current="0" lineColor="#07bfa6" :activeStyle="{ color: '#0a9a87' }"></uv-tabs>
									<uv-tabs :list="jiage_list" @click="jiage_ck" lineColor="#07bfa6" :activeStyle="{ color: '#0a9a87' }"></uv-tabs>
									<uv-tabs :list="banben_list" @click="banben_ck" lineColor="#07bfa6" :activeStyle="{ color: '#0a9a87' }"></uv-tabs>

									<view class="uv-page__tag-container" :style="{marginTop:'10px',marginLeft:'10px', display: 'flex', 'flex-wrap': 'wrap', 'justify-content': 'flex-start', 'gap': '8px' }">
									
									
									<!--uv-tags 
									    v-for="(duan,index) in duan_list" 
									    :key="index" 
									    @click="banben_s(index,duan)"
										:text="duan.name" 
										:plain="!duan.checked"
										type="error" 
										:name="index" 
										>
									</uv-tags-->
								</view>
									
									<uv-tabs :list="zhishi_daohang_list" @click="yiji2" lineColor="#07bfa6" :activeStyle="{ color: '#0a9a87' }"></uv-tabs>
									<uv-tabs v-if="zhishi_daohang_list.length > 0 " :key="'erji-' + activeIndex.id"  :current="xuanzezhi" :list="erjidata" @click="erji2" lineColor="#07bfa6" :activeStyle="{ color: '#0a9a87' }"></uv-tabs>
								</view>
							<!--内容列表-->
							<uv-row :customStyle="{ 'margin-top': '180px', 'flex-wrap': 'wrap' }"  :gutter="20" wrap>
									<uv-col v-for="(list,index) in zhishi_list" :span="kuan_quan">
										<view class="beijing-140 d-kuan d-buhuanhang d-xia-20" @click="yingyong_url(list.id)">
											<!--图片-->
											<view class="d-zuo d-tu-kuan-140 d-piaofu-0">
												<!--view v-if="list.moshi==0" class="d-piaofu-1">免费</view>
												<view v-else class="d-piaofu-1">付费</view-->
												<uv-image :src="list.thumb" width="140rpx" height="140rpx" radius="10"></uv-image>
											</view>
											<view class="d-kuan d-neikuang-10 d-piaofu-jiage-0">
												<view v-if="list.price > 0" class="d-piaofu-jiage-4">{{ list.price}}</view>
											    <view v-else class="d-piaofu-jiage-4">免费</view>
												<view class="d-chaochu-1 d-jiacu">{{list.title}}</view>
												<view class="d-chaochu-1 d-ziti-12 d-wenzi-yanse-hui d-shangxia-5">
													{{list.jianjie}}
												</view>
												<view class="d-chaochu-1 d-ziti-12 d-wenzi-yanse-hui d-shangxia-10">
													<uv-icon name="arrow-right" color="#a7a7a7" labelColor="#a7a7a7" size="10" :label="list.mingcheng"></uv-icon>
												</view>
											</view>
										</view>


									</uv-col>
									<!-- 加载状态提示 -->
  									<view class="d-kuan d-juzhong d-shangxia-20" v-if="isLoading">
  									  <uv-loading></uv-loading>
  									</view>
  									<view class="d-kuan d-juzhong d-shangxia-20 d-wenzi-hui" v-if="noMoreData">
  									  <uv-divider text="没有更多数据了"></uv-divider>
  									</view>

								</uv-row>

						</view>
					</uv-col>

					<uv-col span="3" class="pc-yincang">

					</uv-col>
				</uv-row>
				<!--右边内容-->

			</view>
			<!--下面内容-->

		</view>
	</view>
</template>

<script>
    import Api from '@/utils/api.js';
	//import Apidiy from '@/utils/yingyong_api.js';
	import YingyongApi from '@/utils/yingyong_api.js';
	//var wxRequest_2 = require("../../utils/wxRequest.js");
	import { getRequest,postRequest } from '@/utils/wxRequest.js';
    //引入组件 d-toubu
    import DToubu from '@/components/d-toubu/d-toubu.vue'
	import Ddaohang from '@/components/d-daohang/d-daohang.vue'
	export default {
		data() {
			return {
				title: 'Hello',
				leftNavHeight: '500rpx', // 默认高度
				daohangkuanzhi: '50%', // 默认值
				rightColWidth: '300px', // 默认宽度
				rightColLeft: '0px', // 添加这个属性
				daohang_top:'60',

				daohang: [
					{
						id:'1',
						name: '首页', 
						icon: 'home',

					},
					{
						id:'2',
						name: '心理学',
						icon: 'home',
						erji: [	
							{id: '2-1', name: '情绪管理'},
					    	{id: '2-2', name: '心理疗愈'},
					    	{id: '2-3', name: '行为心理学'}
						]
					},
					
				],
				denglu_xianshi:0, // 登录显示
				erjidata:[],
				erjiindex:'',//当前激活的二级菜单索引
				activeIndex: -1, // 当前激活的一级菜单索引
				zhishi_daohang_list:[], // 知识导航列表
				zhishi_list:[], // 知识列表
				xuanzezhi:0, // 知识列表
				daohanggaoHeight: '94px', // 导航栏高度

				currentPage: 1, //下拉加载
    			pageSize: 12, //下拉加载
    			isLoading: false, //下拉加载
    			noMoreData: false, //下拉加载
				kuan_shu: 8, //格大小
				kuan_quan:6,//格大小
				yemiangao:70, //顶部导航栏
				yingyong_daohang_list:[],
				jiage_list:[
					{
						id:0,
						name:'全部'
					},
					{
						id:1,
						name:'免费'
					},
					{
						id:2,
						name:'50~100元'
					},
					{
						id:3,
						name:'100~300元'
					},
					{
						id:4,
						name:'300~1000元'
					},
					{
						id:5,
						name:'1000~3000元'
					},
					{
						id:6,
						name:'3000~10000元'
					},
					{
						id:7,
						name:'10000元以上'
					},
					
				],
				banben_list:[
					{
						id:0,
						name:'全部'
					},
					{
						id:1,
						name:'3.x'
					}
				],
				duan_list:[
					{
						id:1,
						name:'全端支持',
						checked: false
					},
					{
						id:2,
						name:'PC',
						checked: false
					},
					{
						id:3,
						name:'H5',
						checked: false
					},
					{
						id:4,
						name:'安卓APP',
						checked: false
					},
					{
						id:5,
						name:'苹果APP',
						checked: false
					},
					{
						id:6,
						name:'鸿蒙APP',
						checked: false
					},
					{
						id:7,
						name:'微信小程序',
						checked: false
					},
					{
						id:8,
						name:'抖音小程序',
						checked: false
					},
					{
						id:8,
						name:'百度小程序',
						checked: false
					},
					
				],
				shuxing_list:[
					{
						id:0,
						name:'全部'
					},
					{
						id:1,
						name:'安装'
					},
					{
						id:2,
						name:'开发'
					},
					{
						id:3,
						name:'修改'
					}
				],
				fenleizhi:0,
				jiagezhi:0,
				banbenzhi:0,
				

			}
		},
		components: {
  		    DToubu,
			Ddaohang
  		},
		computed: {
		  	yidenglu() {
		  	  return (this.$store.state.memberuid && this.$store.state.keycode) ? 1 : 0;
		  	},
		  	touxiang() {
		  	  return this.$store.state.member? this.$store.state.member.avatar : '';
		  	},
		  	name1() {
		  	  return this.$store.state.member? this.$store.state.member.name : '';
		  	},
			uid() {
		  	  return this.$store.state.memberuid;
		  	},
		  	keycode() {
		  	  return this.$store.state.keycode;
		  	}
		},

		onReachBottom() {
  			this.onReachBottom(); //下拉加载分页
  		},
		mounted() {
			//var m = this.$store.state;
			//console.log('mmmm',m);
			
    	    this.setLeftNavHeight();
			this.getRightColWidth(); // 添加这行
			
    	    // 添加窗口大小变化监听
    	    window.addEventListener('resize', this.setLeftNavHeight);
			window.addEventListener('resize', this.getRightColWidth); // 添加窗口大小变化监听
			this.$nextTick(() => {
				// 计算高度
				//this.calcDaohanggaoHeight();
    		});

    	},
    	beforeDestroy() {
    	    // 移除监听
    	    window.removeEventListener('resize', this.setLeftNavHeight);
			window.removeEventListener('resize', this.getRightColWidth); // 添加这行
			this.clearAllAudio();
    	},
		onShow() {
			this.yingyong_daohang_api();
			//var m = this.$store.state;
			//console.log('12300000:', this.$store.state.memberuid);
			
			var e = this;
			this.zhishi_list_api(e,false);
		},
		onLoad() {
			
		},
		methods: {
			fenlei_ck(data){
				console.log('data',data);
				this.fenleizhi = data.id;
				var e = this;
			    this.zhishi_list_api(e,false);
			},
			jiage_ck(data){
				console.log('jiage',data.id);
				this.jiagezhi = data.id;
				var e = this;
				this.zhishi_list_api(e,false);
			},
			banben_ck(data){
				console.log('banben',data.id);
				this.banbenzhi = data.id;
				var e = this;
				this.zhishi_list_api(e,false);
			},
			// 多选端
			banben_s(index,name) {
				console.log('dianj',name);
				// 切换选中状态
				this.duan_list[index].checked = !this.duan_list[index].checked;
			},
			//第一级栏目
			diyiji(e){
				if(e.index==0){
					uni.navigateTo({
						url: '/pages/yingyong/index',
						fail: (err) => {
							console.error('跳转失败:', err)
						}
					})
				} else if (e.index==1) {
					uni.navigateTo({
						url: '/pages/yingyong/chajian',
						fail: (err) => {
							console.error('跳转失败:', err)
						}
					})
				} else if (e.index==2) {
					uni.navigateTo({
						url: '/pages/yingyong/zujian',
						fail: (err) => {
							console.error('跳转失败:', err)
						}
					})
					
				} else if (e.index==3) {
					uni.navigateTo({
						url: '/pages/yingyong/muban',
						fail: (err) => {
							console.error('跳转失败:', err)
						}
					})
					
				} else if (e.index==4) {
					uni.navigateTo({
						url: '/pages/yingyong/fuwu',
						fail: (err) => {
							console.error('跳转失败:', err)
						}
					})
				}
			},
			jiage(){
				
			},
			// 发布登录
			denglu_fabu(){
				if(this.yidenglu == 0){
					this.denglu_xianshi = 1;
				} else {
					this.denglu_xianshi = 0;	
				}
				
				console.log('发布登录',this.denglu_xianshi);
				
			},
			// 更新判断是否登录
			handleUpdateDengluxianshi(value) {
			  	this.denglu_xianshi = value;
			  	console.log('关闭登录',this.denglu_xianshi);
			},
			// 退出登录
			handleLogout() {
 			  // 用户退出时关闭阅读界面
 			  this.yuedu = false;

 			  // 停止并销毁音频
 			  if(this.$refs.audioPlayer) {
 			    const audioCtx = this.$refs.audioPlayer.getAudioContext();
 			    audioCtx.stop();
 			    audioCtx.destroy();
 			  }
			   //this.getDianzan_panduan_api(); // 退出登录后重新获取点赞状态
			   //this.getShoucang_panduan_api(); // 退出登录后重新获取收藏判断
 			},
			// 登录
			handleLogin() {
				//this.getDianzan_panduan_api(); // 登录后重新获取点赞状态
				//this.getShoucang_panduan_api(); //登录后重新获取收藏判断
			},



			getRightColWidth() {
				// 获取系统信息
				const query_2 = uni.createSelectorQuery().in(this);
				const systemInfo = uni.getSystemInfoSync();
        	    // 计算可用高度（可根据实际需求调整计算方式）
        	    const windowWidth = systemInfo.windowWidth;
				var d = this;            
               this.$nextTick(() => {
					if(windowWidth >=920){
						query_2.select('#daohangkuan').boundingClientRect(data => {
    		    		    if (data) {
    		    		        this.daohangkuanzhi = `${data.width}px`;
    		    		    }
    		    		}).exec();

						d.gao = 20;
						d.kuan_shu = 8;
						d.kuan_quan = 6;
						d.swiperHeight = '250';
						d.daohang_top = '60';
						d.yemiangao = '70';
					} else {
						d.gao = 10;
						d.kuan_shu = 12;
						d.kuan_quan = 12;
						d.swiperHeight = '120';
						d.daohang_top = '80';
						d.yemiangao = '80';
						this.daohangkuanzhi = '100%'; // 或其他适合的默认值


					}

                 const query = uni.createSelectorQuery().in(this);
                 query.select('#youbiankuan').boundingClientRect(rect => {
                   if (rect) {
                     /*this.rightColWidth = `${rect.width+20}px`;
                     console.log('右侧列宽度:', this.rightColWidth);*/
					 	this.rightColWidth = `${rect.width-80}px`;
                    	this.rightColLeft = `${rect.left}px`; // 计算左侧位置
                    	console.log('右侧列位置:', this.rightColWidth);
                   }
                 }).exec();
               });
            },

			calcDaohanggaoHeight() {
				this.$nextTick(() => {

    		    	const query = uni.createSelectorQuery().in(this);
    		    	query.select('#daohanggao').boundingClientRect(data => {
    		    	    if (data) {
    		    	        this.daohanggaoHeight = `${data.height}px`;
							console.log('导航栏高度:', this.daohanggaoHeight);
    		    	    }
    		    	}).exec();
				});
    		},

			//首页
			shouye(){
				uni.navigateTo({
					url: '/pages/index/index',
					fail: (err) => {
						console.error('跳转失败:', err)
					}
				})
			},
			// ...其他方法保持不变...
			yiji(index) {
    		  // 切换激活的一级菜单
    		  this.activeIndex = index;
    		  // 根据激活的一级菜单显示对应的二级菜单
			  if(index == 0){
				  this.erjidata = [];
			  }else{
				  this.erjidata = this.zhishi_daohang_list[index].son;
			  }
			  const data = {
				  categories:index
			  }
			  this.zhishi_list_api(data,true);
    		  //this.erjidata = this.zhishi_daohang_list[index].son;
			  console.log(this.erjidata);
    		},
			erji(index) {
    		  // 切换激活的二级菜单
    		  this.erjiindex = index;

			  const data = {
				  categories:index
			  }
			  this.zhishi_list_api(data,true);
			  //console.log(index);

    		},
			yiji2(index) {
				this.xuanzezhi = 0;
				
				console.log(this.xuanzezhi);
    		  // 切换激活的一级菜单
			  setTimeout(() => {

    			  	this.activeIndex = index;
    			  	// 根据激活的一级菜单显示对应的二级菜单
				  	if(index.id == 0){
						  this.erjidata = [];
				  	}else{
						  this.erjidata = this.zhishi_daohang_list[index.index].son;
						  
				  	}
				  	const data = {
						  categories:index.id,
						  page: 1 // 重置页码
				  	}
					this.currentPage = 1; // 重置页码
					this.noMoreData = false; // 重置无更多数据标志
				  	this.zhishi_list_api(data,false);
					  this.calcDaohanggaoHeight();
				}, 300);
    		  
			  
    		},
			erji2(index) {
				this.xuanzezhi = index.index;
    		  // 切换激活的二级菜单
    		  this.erjiindex = index.id;
			  this.activeIndex = index;

			  const data = {
				  categories:index.id,
				  page: 1 // 重置页码
			  }
			    this.currentPage = 1; // 重置页码
				this.noMoreData = false; // 重置无更多数据标志

			  this.zhishi_list_api(data,false);
			  console.log('二级分类：',index);

    		},

			toggleErji(index) {
				
    		  if (this.activeIndex === index) {
    		    // 点击已激活的菜单，收起二级菜单
    		    this.activeIndex = -1;
    		    this.currentErji = [];
    		  } else {
    		    // 点击新的菜单，显示对应的二级菜单
    		    this.activeIndex = index;
    		    this.currentErji = this.zhishi_daohang_list[index].erji;
    		  }
    		},
			setLeftNavHeight() {
        	    // 获取系统信息
        	    const systemInfo = uni.getSystemInfoSync();
        	    // 计算可用高度（可根据实际需求调整计算方式）
        	    const windowHeight = systemInfo.windowHeight;
        	    const headerHeight = 70; // 头部高度估计值
        	    const calculatedHeight = windowHeight - headerHeight;
				
        	    // 设置左边导航高度
        	    this.leftNavHeight = `${calculatedHeight}px`;
        	},
			zhishi(id){
				console.log(id);
				try {
  				  uni.navigateTo({
  				    url: '/pages/zhishi/index?id='+id,
  				    fail: (err) => {
  				      console.error('跳转失败:', err)
  				    }
  				  })
  				} catch (e) {
  				  console.error('跳转异常:', e)
  				}
			},
			
			handleLoginSuccess() {
  			  this.$forceUpdate();
  			},
			// 导航接口
			yingyong_daohang_api(){
				var get_data = getRequest(YingyongApi.getYingyong_daohang());
				var res;
				get_data.then(duixin_data => {
					res = duixin_data;
					if(res.data.code) {
						// 转换时间戳为日期格式
						//var list = res.data ? res.data.data : {};
						var list = res.data && res.data.data ? res.data.data.lisi2 : [];
						// 在 list 前面添加插入一个新的数据“首页”
						/*list.unshift({
            			    id: '0',
							pid: '0',
            			    name: '首页',
            			    icon: 'home',
            			    son: [] // 首页没有二级菜单
            			});*/
        				this.yingyong_daohang_list = list;
						console.log(list);
					} else {
						uni.showToast({
							icon: 'none',
							title: '获取失败，请稍后重试'
						});
					}
				})
			},
			//列表接口
			zhishi_list_api(data, isLoadMore = false){
				if (!data) data = {};
				if (!data) data = {};
		  		if (!data.page) data.page = 1;
		  		//if (!data.categories) data.categories = 0;
		  		if (!data.search) data.search = '';
				// 分类
				if(this.fenleizhi){
					data.fuwufenlei = this.fenleizhi;
				} else {
					data.fuwufenlei = 0;
				}
				//价格
				if(this.jiagezhi){
					data.jiage = this.jiagezhi;
				} else {
					data.jiage = 0;
				}
				//版本
				if(this.banbenzhi){
					data.banben = this.banbenzhi;
				} else {
					data.banben = 0;
				}
				data.categories = 4;
				var get_data = getRequest(YingyongApi.getYingyong_list(data));
				var res;	
				get_data.then(duixin_data => {
					res = duixin_data;	
					if(res.data.code) {
						// 转换时间戳为日期格式
						var list = res.data? res.data.data.list : {};
						//this.zhishi_list = list;
						if (isLoadMore) {
        				  // 加载更多时追加数据
        				  this.zhishi_list = [...this.zhishi_list, ...list];
        				} else {
        				  // 非加载更多时替换数据
        				  this.zhishi_list = list;
        				}	
						// 判断是否还有更多数据
						this.noMoreData = list.length < this.pageSize;
					} else {
						uni.showToast({
							icon: 'none',
							title: '获取失败，请稍后重试'
						});
					}
					this.isLoading = false;
				}).catch(() => {
    			  this.isLoading = false;
    			  if (isLoadMore) {
    			    this.currentPage--; // 加载失败时回退页码
    			  }
    			});
			},
			// 下拉加载更多
  			onReachBottom() {
  			  if (this.isLoading || this.noMoreData) return;
  			  this.isLoading = true;
  			  this.currentPage++;
			  // 获取当前激活的分类ID
    			const currentCategoryId = this.activeIndex.id >= 0 ? this.activeIndex.id : 0;
    			

  			  const data = {
  			    page: this.currentPage,
  			    categories: currentCategoryId
  			  };
			  console.log('分页',this.activeIndex.id);
  			  this.zhishi_list_api(data, true); // 添加第二个参数表示是加载更多
  			},
			// 关闭播放音频
			clearAllAudio() {
    		  if(this.$refs.audioPlayer) {
    		    const audioCtx = this.$refs.audioPlayer.getAudioContext();
    		    audioCtx.stop();
    		    audioCtx.destroy();
    		  }
    		},
			// 跳转到应用页面
			yingyong_url(id){
				uni.navigateTo({
					url: '/pages/yingyong/id?id='+id,

					fail: (err) => {
						console.error('跳转失败:', err)
					}
				})
			}


		}
	}
</script>

<style>

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		/*justify-content: center;*/
		/*background-color: #f3f3f3;*/
		
		height: 100vh; /* 添加全屏 */
		width: 100vw; /* 添加全屏*/
		box-sizing: border-box;

	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	/* 屏幕宽度大于786px时隐藏uni-page-head */
    @media (min-width: 786px) {
        uni-page-head {
            display: none !important;
            opacity: 0 !important;
            height: 0 !important;
            visibility: hidden !important;
        }
    }
	/*.d-daohang{
		
		width: 600rpx;
	}*/
	/* 添加在style部分 */
	.d-daohang-zuo {
    position: fixed;
    left: 0;
    
    width: 180rpx;
    height: calc(100vh - 70rpx);
    background: #f5f5f5;
    z-index: 100;
	box-shadow: 2px 0 5px rgba(177, 177, 177, 0.1);
  }
  
  .d-daohang-you {
    position: fixed;
    left: 180rpx;
    
    width: 180rpx;
    height: calc(100vh - 70rpx);
    background: #fff;
    z-index: 100;
    box-shadow: 2px 0 5px rgba(0,0,0,0.1);
  }
  .d-erji-item {
    height: 60rpx;
    line-height: 60rpx;
    padding: 0 20rpx;
   /* border-bottom: 1px solid #f0f0f0;*/
  }
  
  .d-erji-item:active {
    background: #f5f5f5;
  }
  .you-guding-1 {
		position: fixed;
		top: 0; /* 根据头部高度调整 */
		/*box-shadow: -2px 0 5px rgba(0,0,0,0.1);*/
		transition: all 0.3s ease;
	}
    .you-guding-2 {
		position: fixed;

		top: 60px; /* 根据头部高度调整 */
		/*box-shadow: -2px 0 5px rgba(0,0,0,0.1);*/
		transition: all 0.3s ease;
	}
	.uv-page__tag-item {
		margin-top:20px;
		margin-left: 20px;
		display: flex; flex-wrap: wrap; justify-content: flex-start; gap: 8px
	}


	
</style>
